﻿using PEUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PECommon
{
    /// <summary>
    /// 进程启动器
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ProcessLauncher<T> where T : ServerProcess<T>, new()
    {
        private string mCmdArgs = string.Empty;
        private ConsoleCMD mCMDType;

        public ProcessLauncher(T server,int defaultID,bool ignoreInput, string?[] args) 
        {
            string argsContent = string.Empty;
            if (!ignoreInput)
            {
                if (args.Length == 0)
                {
                    while (true)
                    {
                        string? str = Console.ReadLine();
                        if (!string.IsNullOrEmpty(str))
                        {
                            if (int.TryParse(str, out var id))
                            {
                                defaultID = id;
                            }
                            else
                            {
                                Console.WriteLine("输入的ID不合法!");
                            }

                        }
                        else
                        {
                            break;
                        }
                    }

                }
                else
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        argsContent += args[i];
                    }
                    if (int.TryParse(args[0], out int id))
                    {
                        defaultID = id;
                    }
                    
                }


                
            }

            LogConfig config = new LogConfig();
            config.loggerEnum = LoggerType.Console;
            config.saveName = $"{typeof(T).Name}_{defaultID}.txt";
            PELog.InitSettings(config);
            this.LogMagenta("Args:" + argsContent);
            server.ProcessID = defaultID;
            PELog.LogGreen($"Launch {typeof(T).Name}===>ID:{server.ProcessID}");

            Task.Run(HandleConsoleCMD);
            server.Awake();

            while (true)
            {
                server.Update();
                Thread.Sleep(30);
                if (mCMDType != ConsoleCMD.None)
                {
                    if (mCMDType == ConsoleCMD.Quit)
                    {
                        server.Destory();
                        break;
                    }
                    else
                    {
                        server.HandleConsoleCMD(mCMDType, mCmdArgs);
                        mCMDType = ConsoleCMD.None;
                        mCmdArgs = string.Empty;
                    }

                }

            }

        }

        private void HandleConsoleCMD()
        {
            //子线程
            while (true)
            {
                if (mCMDType == ConsoleCMD.Quit)
                {
                    Thread.Sleep(1000);
                    continue;
                }
                else
                {
                    string? str = Console.ReadLine();
                    if (!string.IsNullOrEmpty(str))
                    {
                        ParseConsoleCMD(str);
                    }
             
                }
                
            }
            
        }

        private void ParseConsoleCMD(string inputStr)
        {
            string[] args = inputStr.Split(' ');
            try
            {
                mCMDType = (ConsoleCMD)Enum.Parse(typeof(ConsoleCMD), args[0]);
                if (mCMDType < ConsoleCMD.Max)
                {
                    PELog.LogCyan($"Input CMD [{mCMDType}]");
                    if (args.Length > 1)
                    {
                        mCmdArgs = args[1];
                    }
                }
            }
            catch (Exception)
            {
                this.Warn($"参数解析错误，输入的参数[{inputStr}]不合法!");
                mCMDType = ConsoleCMD.None;
                mCmdArgs = string.Empty;
            }
            
            
        }
    }
    

 
}
